package customer

import (
	"context"
	"github.com/google/wire"
	"sieve_admin_server_userpass/app/api/util"
	"sieve_admin_server_userpass/app/contextx"
	"sieve_admin_server_userpass/app/schema"
	"sieve_admin_server_userpass/app/server"
	serviceCustomer "sieve_admin_server_userpass/app/service/customer"
)

var (
	_               util.API = (*BusinessTypeAPI)(nil)
	BusinessTypeSet          = wire.NewSet(wire.Struct(new(BusinessTypeAPI), "*"))
)

type BusinessTypeAPI struct {
	util.EmptyAPI
	BusinessTypeSrv *serviceCustomer.BusinessTypeSrv
}

func (a *BusinessTypeAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "businessType",
			Title: "业务类型配置", Perms: []*server.Perm{
				{Name: "query", Title: "搜索"},
				{Name: "queryTypes", Title: "获取类型列表"},
				{Name: "add", Title: "添加"},
				{Name: "del", Title: "删除"},
				{Name: "edit", Title: "编辑"},
				{Name: "updateBatch", Title: "批量更新"},
				{Name: "enable", Title: "启用"},
				{Name: "disable", Title: "禁用"},
				{Name: "export", Title: "导出"},
			},
			Order: 5,
		},
	}
}

func (a *BusinessTypeAPI) Cron() {

	_ = a.BusinessTypeSrv.Cron()
}

func (a *BusinessTypeAPI) API_add_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("name"),
				contextx.NewVar("desc"),
				contextx.NewVar("sieve_source_config_id", true),
				contextx.NewVar("unit_price", true),
			},
		)
	)

	return a.BusinessTypeSrv.Create(context.Background(), ctx.Uid, reqArgs)
}

func (a *BusinessTypeAPI) API_query_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("name"),
				contextx.NewVar("sieve_source_config_id", true),
			},
		)

		p = schema.PaginationParam{
			PageIndex: util.ToInt(ctx.Args.Get("page_index")),
			PageSize:  util.ToInt(ctx.Args.Get("page_size")),
		}
	)

	return a.BusinessTypeSrv.Query(context.Background(), ctx.Uid, reqArgs, p)
}

func (a *BusinessTypeAPI) API_queryTypes_list(ctx *server.Context) interface{} {

	return a.BusinessTypeSrv.GetBusinessTypes(context.Background())
}

func (a *BusinessTypeAPI) API_edit_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("name"),
				contextx.NewVar("desc"),
				contextx.NewVar("unit_price", true),
				contextx.NewVar("sieve_source_config_id", true),
			},
		)
	)

	return a.BusinessTypeSrv.Update(context.Background(), ctx.Uid, reqArgs)
}

func (a *BusinessTypeAPI) API_del_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.BusinessTypeSrv.Delete(context.Background(), uint64(ctx.Uid), reqArgs)
}

func (a *BusinessTypeAPI) API_updateBatch_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("ids"),
				contextx.NewVar("sieve_source_config_id", true),
			},
		)
	)

	return a.BusinessTypeSrv.UpdateBatch(context.Background(), ctx.Uid, reqArgs)
}

func (a *BusinessTypeAPI) API_enable_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.BusinessTypeSrv.Enable(context.Background(), ctx.Uid, reqArgs)
}

func (a *BusinessTypeAPI) API_disable_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.BusinessTypeSrv.Disable(context.Background(), ctx.Uid, reqArgs)
}

func (a *BusinessTypeAPI) API_export_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("ids"),
			},
		)
	)

	return a.BusinessTypeSrv.Export(context.Background(), ctx.Uid, reqArgs)
}
